﻿using B.T.BasicData.Domain.RBAC;
using B.T.BasicData.ErrorCode;
using B.T.BasicData.Write_Api.DTO;
using B.T.BasicData.Write_Api.RBAC.IServices;
using B.T.BasicData.Write_Api.RBAC.Services;
using Humanizer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.T.BasicData.Write_Api.Controllers
{
    /// <summary>
    /// RBAC系统管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PermissionsController : ControllerBase
    {
        private readonly ILogger<PermissionsController> logger;
        private readonly IPermissionsServices rBACServices;

        public PermissionsController(ILogger<PermissionsController> logger,IPermissionsServices rBACServices)
        {
            this.logger = logger;
            this.rBACServices = rBACServices;
        }


        /// <summary>
        /// 权限添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        [Authorize]
        public async Task<ApiResult> AddPermissions(AddPermissionsDto dto)
        {
            try
            {
                logger.LogInformation("添加操作");
                return await rBACServices.AddPermissions(dto);
            }
            catch (Exception ex)
            {

                logger.LogError("权限添加报错异常" + ex.Message);
                throw ex;
            }
        }


        /// <summary>
        /// 权限显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<ApiResult<List<Permissions>>> SreachPermissions(string ? PermissionName)
        {
            try
            {
                logger.LogInformation("权限显示操作");
                return await rBACServices.SreachPermissions(PermissionName);
            }
            catch (Exception ex)
            {

                logger.LogError("权限显示报错异常" + ex.Message);
                throw ex;
            }
        }


        /// <summary>
        /// 查找权限所有名称
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<ApiResult<List<SearchPermissionsDto>>> SearchPermissionsinfo()
        {
            try
            {
                return await rBACServices.SearchPermissionsinfo();
            }
            catch (Exception ex)
            {

                logger.LogError("=>" + ex.Message);
                throw ex;
            }
        }


        /// <summary>
        /// 权限删除
        /// </summary>
        /// <returns></returns>
        [HttpDelete]
        [Authorize]
        public async Task<ApiResult> DelePermissions(DelePermissionsdto dto)
        {
            try
            {
                logger.LogInformation("权限数据删除操作");
                return await rBACServices.DelePermissions(dto);
            }
            catch (Exception ex)
            {

                logger.LogError("权限数据删除报错异常" + ex.Message);
                throw ex;
            }
        }

        /// <summary>
        /// 修改权限信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut]
        [Authorize]
        public async Task<ApiResult> UpdatePermissions(UpdatePermissionsDto dto)
        {
            try
            {
                logger.LogInformation("权限数据修改操作");
                return await rBACServices.UpdatePermissions(dto);
            }
            catch (Exception ex)
            {

                logger.LogError("权限数据修改报错异常" + ex.Message);
                throw ex;
            }
        }
    }
}
